package co.yixiang.web.controller.tool;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import co.yixiang.shop.service.IStoreGoodImageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import co.yixiang.common.core.controller.BaseController;
import co.yixiang.common.core.domain.AjaxResult;
import co.yixiang.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

/**
 * swagger 用户测试方法
 * 
 * @author ruoyi
 */
@Api("用户信息管理")
@RestController
@RequestMapping("/test/user")
public class TestController extends BaseController
{
    private final OssTemplate ossTemplate;
    private String prefix = "shop/image";

    @Autowired
    public IStoreGoodImageService storeGoodImageService;

    public TestController(OssTemplate ossTemplate) {
        this.ossTemplate = ossTemplate;
    }


    ///  跳转到  image
    @RequiresPermissions("shop:goods_image:view")
    @GetMapping()
    public String goods()
    {
        return prefix + "/image";
    }

//    @ApiOperation("获取图片组列表")
//    @GetMapping("/lists")
//    @RequiresPermissions("shop:image:list")
//    @PostMapping("/lists")
//    @ResponseBody
//    public TableDataInfo list()
//    {
//        startPage();
//        List<StoreGoodsImages> list = storeGoodImageService.list();
//        return getDataTable(list);
//    }
//
//
//    @ApiOperation("获取图片组列表 lists1")
//    @GetMapping("/lists1")
//    @RequiresPermissions("shop:image:lists1")
//    @PostMapping("/lists1")
//    @ResponseBody
//    public TableDataInfo lists1()
//    {
//        startPage();
//        List<StoreGoodsImagesVo> list = storeGoodImageService.list1();
//        System.out.println("获取图片组列表 lists1"+list);
//        return getDataTable(list);
//    }



//-----------------------  分界线 -------------------------



    private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();
    {
        users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
        users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/list")
    public AjaxResult userList()
    {
        List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
        return AjaxResult.success(userList);
    }

    @ApiOperation("获取用户详细")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
    @GetMapping("/{userId}")
    public AjaxResult getUser(@PathVariable Integer userId)
    {
        if (!users.isEmpty() && users.containsKey(userId))
        {
            return AjaxResult.success(users.get(userId));
        }
        else
        {
            return error("用户不存在");
        }
    }

    @ApiOperation("新增用户")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer"),
        @ApiImplicitParam(name = "username", value = "用户名称", dataType = "String"),
        @ApiImplicitParam(name = "password", value = "用户密码", dataType = "String"),
        @ApiImplicitParam(name = "mobile", value = "用户手机", dataType = "String")
    })
    @PostMapping("/save")
    public AjaxResult save(UserEntity user)
    {
        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
        {
            return error("用户ID不能为空");
        }
        return AjaxResult.success(users.put(user.getUserId(), user));
    }

    @ApiOperation("更新用户")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody UserEntity user)
    {
        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
        {
            return error("用户ID不能为空");
        }
        if (users.isEmpty() || !users.containsKey(user.getUserId()))
        {
            return error("用户不存在");
        }
        users.remove(user.getUserId());
        return AjaxResult.success(users.put(user.getUserId(), user));
    }

    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
    @DeleteMapping("/{userId}")
    public AjaxResult delete(@PathVariable Integer userId)
    {
        if (!users.isEmpty() && users.containsKey(userId))
        {
            users.remove(userId);
            return success();
        }
        else
        {
            return error("用户不存在");
        }
    }


//      阿里oss  文件上传  测试   begin ============================

    @ApiOperation("单个上传图片信息")
    @PostMapping("/upload")
    public String upload(MultipartFile file) {
        return ossTemplate.upload(file);
    }
    @ApiOperation("上传多个图片信息")
    @PostMapping("/batch")
    public List<String> upload(MultipartFile[] files) {
        return ossTemplate.upload(files);
    }
    @ApiOperation("删除单个图片信息")
    @GetMapping("/delete")
    public void delete(@RequestParam(name = "url") String url) {
        ossTemplate.delete(url);
    }
    @ApiOperation("删除多个图片信息")
    @PostMapping("/deletes")
    public void deletes(@RequestBody List<String> urlList) {
        ossTemplate.delete(urlList);
    }

//      阿里oss  文件上传  测试   end ============================
}

@ApiModel(value = "UserEntity", description = "用户实体")
class UserEntity
{
    @ApiModelProperty("用户ID")
    private Integer userId;

    @ApiModelProperty("用户名称")
    private String username;

    @ApiModelProperty("用户密码")
    private String password;

    @ApiModelProperty("用户手机")
    private String mobile;

    public UserEntity()
    {

    }

    public UserEntity(Integer userId, String username, String password, String mobile)
    {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.mobile = mobile;
    }

    public Integer getUserId()
    {
        return userId;
    }

    public void setUserId(Integer userId)
    {
        this.userId = userId;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getMobile()
    {
        return mobile;
    }

    public void setMobile(String mobile)
    {
        this.mobile = mobile;
    }



}
